package behaviors;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.contacts.ContactPoint;
import org.jbox2d.dynamics.contacts.ContactResult;

public class Fish extends Behavior {
	private long t = 0;

	@Override
	public void onCollide(ContactPoint arg0) {
	}

	@Override
	public void onPersist(ContactPoint arg0) {
	}

	@Override
	public void onRemove(ContactPoint arg0) {
	}

	@Override
	public void onResult(ContactResult arg0) {
	}

	@Override
	public void update(Body b) {
		super.update(b);

		if (System.currentTimeMillis() > t) {
			t = (System.currentTimeMillis() + (long) (Math.random() * 2000f));

			body.applyImpulse(new Vec2((float) ((Math.random() * 20f) - 10f),
					(float) (Math.random() * 2f) - 1f), body.getPosition());

			if (body.getLinearVelocity().x < 0) {
				entity.facing = false;
			} else {
				entity.facing = true;
			}

			// constrain fish movement
			if (body.getPosition().y < 1f) {
				body.applyImpulse(new Vec2(0, 2f), body.getPosition());
			}
		}
	}

	@Override
	public Behavior clone() {
		Fish obj = new Fish();
		obj.t = this.t;
		return obj;
	}

	@Override
	public void init() {
	}
}
